//
//  NewSongNewDVDViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewSongNewDVDViewController.h"
#import <AFNetworking.h>
#import "NewSongsNewDVDCollectionViewCell.h"
#import "PosterDetailViewController.h"
#import "MJRefresh.h"

@interface NewSongNewDVDViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, assign)CGFloat currentOffSit_y;
@property (nonatomic, strong)JHJTabBarController *tabBar;
@property (nonatomic, assign)NSInteger page;

@end

@implementation NewSongNewDVDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.array = [NSMutableArray array];
    self.page = 1;
    AppDelegate *tempAppdelegate = [[UIApplication sharedApplication] delegate];
    self.tabBar = tempAppdelegate.JHJTabBar;
    [self createView];
    [self createData];
    [self createHeader];
    [self createFooter];
}

- (void)createHeader {
    
}

- (void)createFooter {
    NewSongNewDVDViewController *newVC = self;
    [self.collectionView addFooterWithCallback:^{
        newVC.page++;
        [newVC createData];
        
    }];
}

- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *firstStr = @"http://api.dongting.com/misc/album/new?";
    NSString *lastStr = @"&size=30&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
    NSString *urlStr = [NSString stringWithFormat:@"%@page=%ld%@",firstStr, self.page, lastStr];
//    @"http://api.dongting.com/misc/album/new?page=1&size=30&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100"
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
            SongsModel *model = [[SongsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        [self.collectionView footerEndRefreshing];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        

    }];
}
- (void)createView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    flowLayout.itemSize = CGSizeMake((WIDTH - 50) / 2, (WIDTH - 50) / 2 + 50);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 20;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    [self.collectionView registerClass:[NewSongsNewDVDCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([NewSongsNewDVDCollectionViewCell class])];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PosterDetailViewController *detailsVC = [[PosterDetailViewController alloc] init];
    SongsModel *model = [self.array objectAtIndex:indexPath.row];
    detailsVC.value = model.albumId;
    [self.navigationController pushViewController:detailsVC animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewSongsNewDVDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([NewSongsNewDVDCollectionViewCell class]) forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y > self.currentOffSit_y && scrollView.contentOffset.y <= 1394) {
        [self.tabBar hiddenTabBarWithAnimation:YES];
    } else if (scrollView.contentOffset.y < self.currentOffSit_y && scrollView.contentOffset.y > 0) {
        [self.tabBar showTabBarWithAnimation:YES];
    }
    self.currentOffSit_y = scrollView.contentOffset.y;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
